\documentclass{article}

\usepackage[top=25mm,bottom=25mm,left=25mm,right=25mm]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{subfig}
\usepackage{epstopdf}
\usepackage[framed,autolinebreaks,useliterate]{mcode}
\setlength{\parindent}{0pt}

\begin{document}

\title{ESP project - DTMF signal coding and decoding}
\author{Floris van Nee \& Simon Dirlik}

\maketitle

\section{Questions}
\subsection{} %1
The code is shown below, the comments explain each line.
\begin{lstlisting}
	FS=8000;
	sigma2=0.1;
	RMSsin=1/sqrt(2); % RMS of a sine with amplitude 1
	silence=zeros(1,800); % 8000 is 1s, 800 is 0.1s.
	SNR=20*log10(RMSsin/sigma2); % signal-to-noise ratio
	seq=[tone(1) silence tone(2) silence tone(3) silence tone(4)]; % create sequence of tones separated by silences.
	seqn=awgn(seq,SNR); % add white gaussian noise, with signal-to-noise ratio SNR.
	wavplay(seqn,FS);
\end{lstlisting}
\begin{lstlisting}
	function out=tone(digit)
	% tone - generates tone for each digit
	%        *=10, 0=11 and #=12

	FS=8000;
	X=0:1/FS:0.2; % time runs for 0.2s
	high=[1209 1336 1477]; % high frequencies
	low=[697 770 852 941]; % low frequencies
	high_index=mod(digit,3);% the high frequency is chosen from the vector using the modulo 3 of the digit
	if high_index==0 % unless modulo 3 is zero, then it is supposed to be 3.
		high_index=3;
	end
	hf=high(high_index); 
	hs=sin(2*pi*hf*X);
	lf=low(ceil(1/3)); % the low frequency is chosen from the vector by dividing the digit by 3 and rounding up.
	ls=sin(2*pi*lf*X);

	out=ls+hs;
\end{lstlisting}
The tones are easily audible for anyone and each button in the same column produces the same tone.
\subsection{} %2
By calculating the Fourier transform you get two peaks at the sines that were used to generate the tone. We can filter the input signal using low-pass filters or band-pass filters, to get rid of these peaks. In case of the low-pass filter you would have to multiply the input signal with a local oscillator for each of the seven frequencies and then use the filter. Only the two sines that make up the tone will have significant output. In case of the bandpass filter you could simply make seven filters with the center frequencies corresponding to the frequencies of the DTMF. Again, only the sines that make up the input will have significant output.
This output can be fed to a simple digital circuit which decides which button corresponds to the measured frequencies.
(demux)

\subsection{} %3
We need to design a bandpass filter that can seperate each of the frequencies given in the specification of the DTMF, the smallest difference between each of the frequencies is 73Hz. The filter that generally gives the lowest order is the elliptic (Cauer) filter, this was confirmed by our tests. We will design this filter using the matlab functions \verb|ellipord| and \verb|ellip|. The code of the function that creates the filter, including explanation, is shown below.
\begin{lstlisting}
	function [b,a]=ellipfilter(freq)

	center = freq/4000;
	Wp = [center-0.001 center+0.001]; % use center frequency normalized to 1pi (2*f/FS)
	Ws = [center-73/4000 center+73/4000]; % and passband boundaries of center-73 and center+73
	Rp = 0.2; % passband ripple
	Rs = 1; % stopband attenuation
	% Cauer (elliptic) filter
	[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % estimates the lowest possible order.
	[b,a] = ellip(n,Rp,Rs,Wn); % use the parameters calculated by ellipord. The first parameter, n, of ellip specifies the order of the filter, for a bandpass filter it makes a filter of order 2*n.
\end{lstlisting}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{freqz.eps}
		\caption{The frequency response of the Cauer filter of order 2}
		\label{fig:freqz}
	\end{center}
\end{figure}
Figure \ref{fig:freqz} shows the frequency response of the designed filter with center frequency 941Hz. It shows that at 941Hz-73Hz=868Hz, the attenuation is -32dB, which is enough to supress other signals.

The complete filter bank is shown below. 
\begin{lstlisting}
	lfg = [697 770 852 941]; % Low frequency group
	hfg = [1209 1336 1477];  % High frequency group
	f  = [];
	freqs = [lfg hfg];

	for c=1:4,
		for r=1:3,
			f = [ f [lfg(c);hfg(r)] ]; % make all combinations of frequencies
		end
	end

	inputNoNoise = tone(2); % test input: digit 2
	input = awgn(inputNoNoise, -17); % add noise to input signal
	
	% create filters for each of the different center frequencies.
	[b1,a1] = ellipfilter(freqs(1));
	[b2,a2] = ellipfilter(freqs(2));
	[b3,a3] = ellipfilter(freqs(3));
	[b4,a4] = ellipfilter(freqs(4));
	[b5,a5] = ellipfilter(freqs(5));
	[b6,a6] = ellipfilter(freqs(6));
	[b7,a7] = ellipfilter(freqs(7));
	
	% apply filters to input
	out1 = filter(b1, a1, input);
	out2 = filter(b2, a2, input);
	out3 = filter(b3, a3, input);
	out4 = filter(b4, a4, input);
	out5 = filter(b5, a5, input);
	out6 = filter(b6, a6, input);
	out7 = filter(b7, a7, input);
	
	% calculate energy output of each of the BPF's
	out = [sum(out1.^2), sum(out2.^2), sum(out3.^2), sum(out4.^2), sum(out5.^2), sum(out6.^2), sum(out7.^2)]
\end{lstlisting}
This gives a second order filter for each frequency. The output contains the energy level that comes out of each filter. This value would have to be compared to some threshold, making it a digital 1 if the energy is high enough. The highest noise level that this system can deal with is -17dB. When the noise level is higher, the output of filters corresponding to the incorrect frequencies are sometimes higher than the output of the right filters. An example of the output is shown here using noise level -17dB.
\begin{verbatim}
	out =

  1.0e+003 *

    2.0382    1.3336    1.6145    1.4603    1.0107    1.9772    1.0535
\end{verbatim}
\end{document}
